package com.monitor.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.monitor.dao.ErpCustomerCarMapper;
import com.monitor.dao.ErpCustomerMapper;
import com.monitor.dao.ErpCustomerWarehouseMapper;
import com.monitor.dao.ErpDataErrorMapper;
import com.monitor.dao.ErpMaterielMapper;
import com.monitor.dao.ErpTireArchivesWorkMapper;
import com.monitor.dao.ErpTireSensorMapper;
import com.monitor.entity.ErpCustomer;
import com.monitor.entity.ErpCustomerWarehouse;
import com.monitor.entity.ErpDataError;
import com.monitor.entity.ErpMateriel;
import com.monitor.entity.ErpTireSensor;
import com.monitor.service.ErpDataErrorService;
import com.monitor.util.BlankUtil;
import com.monitor.util.Result;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

/**
 * <p>
 * 改胎信息表 服务实现类
 * </p>
 *
 * @author LX
 * @since 2018-09-18
 */
@Service
public class ErpDataErrorServiceImpl extends ServiceImpl<ErpDataErrorMapper, ErpDataError> implements ErpDataErrorService {
	@Resource
	private ErpCustomerCarMapper erpCustomerCarMapper;
	
	@Resource
	private ErpCustomerWarehouseMapper erpCustomerWarehouseMapper;
	
	@Resource
	private ErpCustomerMapper erpCustomerMapper;
	
	@Resource
	private ErpMaterielMapper erpMaterielMapper;
	
	@Resource
	private ErpTireSensorMapper erpTireSensorMapper;
	
	@Resource
	private ErpTireArchivesWorkMapper erpTireArchivesWorkMapper;

	@Override
	public Result<List<Map<String, Object>>> selectDataErr(String keyWord) {
		// TODO Auto-generated method stub
		keyWord="%"+keyWord+"%";
		List<Map<String, Object>> list=erpCustomerCarMapper.selectByKeyWord(keyWord);
		return new Result<List<Map<String,Object>>>(list, list.size());
	}

	@Override
	public Result<List<Map<String, Object>>> selectEditTire(int custId,String startTime,String endTime) {
		// TODO Auto-generated method stub
		try {
			List<Map<String, Object>> list=new ArrayList<>();
			ErpCustomer cus=erpCustomerMapper.selectById(custId);
			List<ErpCustomerWarehouse> cware =erpCustomerWarehouseMapper.selectList(new EntityWrapper<ErpCustomerWarehouse>().eq("customer_id", custId));
			String ids = "-1";
			for(ErpCustomerWarehouse  cw:cware){
				ids+=","+cw.getId();
			}
			List<Integer> tireList=erpTireArchivesWorkMapper.selectIdByCustId(custId);
			String materielIds=BlankUtil.listToString(tireList);
			StringBuffer sb=new StringBuffer("");
			if(!startTime.equals("")) {
				sb.append(" AND materiel_storage_date>='"+startTime+"' ");
			}
			if(!endTime.equals("")) {
				sb.append(" AND materiel_storage_date<='"+endTime+"' ");
			}
			List<ErpMateriel> Mas=erpMaterielMapper.selectLikeProductionId(ids,materielIds,sb.toString());
			for (ErpMateriel m:Mas) {
				Map<String, Object> map=new HashMap<>();
				map.put("cusName", cus.getCusName());
				map.put("gouTime", m.getMaterielStorageDate());        //购置日期
				map.put("productionId", m.getMaterielProductionId());
				map.put("name", m.getMaterielName());
				map.put("brand", m.getMaterielBrand());
				map.put("spec", m.getMaterielSpecifications());
				map.put("model", m.getMaterielModel());
				map.put("price", Double.valueOf(m.getMaterielStoragePrice()).toString());
				map.put("dot", m.getMaterielDot());
				map.put("cusCode", cus.getCusCode());
				map.put("materielId", m.getId());
				ErpTireSensor tireSensor = erpTireSensorMapper.selectByTireId(m.getId());
				if(tireSensor != null){
					ErpMateriel mSensor = erpMaterielMapper.selectById(tireSensor.getSensorId());
					map.put("taiyaId", mSensor.getMaterielProductionId());
				}else{
					map.put("taiyaId", "");
				}
				list.add(map);
			}
			return new Result<List<Map<String,Object>>>(list, list.size());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return new Result<>(1, e.getMessage());
		}
	}
	
	
}
